// 代码块 importM
import 'package:flutter/material.dart';
import './tools/keepAliveWrapper.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // 去掉debugger图标
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  // 生命周期函数：当组件初始化时触发
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 8, vsync: this);
    // 监听_tabController切换时间
    _tabController.addListener(() {
      // print(_tabController.index); // 获取2次
      if (_tabController.animation!.value ==_tabController.index) {
        print(_tabController.index);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading:
            IconButton(onPressed: () {}, icon: const Icon(Icons.menu)), // 左侧的按钮
        backgroundColor: Colors.red,
        title: const Text("Flutter app"),
        actions: [
          IconButton(onPressed: () {}, icon: const Icon(Icons.search)),
          IconButton(onPressed: () {}, icon: const Icon(Icons.more_horiz)),
        ], // 右侧的按钮
        bottom: TabBar(
            isScrollable: true, // 是否可以滚动
            indicatorColor: Colors.white, // 指示器颜色
            indicatorPadding: const EdgeInsets.all(5), // // 指示器Padding
            labelColor: Colors.yellow, // 选中颜色
            unselectedLabelColor: Colors.white, // 未选中颜色
            controller: _tabController, // 注意：配置controller需要去掉 TabBar的const
            tabs: const [
              Tab(
                child: Text("关注"),
              ),
              Tab(
                child: Text("热门"),
              ),
              Tab(
                child: Text("视频"),
              ),
              Tab(
                child: Text("娱乐"),
              ),
              Tab(
                child: Text("篮球"),
              ),
              Tab(
                child: Text("深圳"),
              ),
              Tab(
                child: Text("疫情"),
              ),
              Tab(
                child: Text("其他"),
              ),
            ]),
      ),
      body: TabBarView(controller: _tabController, children: [
        KeepAliveWrapper( keepAlive: true, child: ListView(
          children: const [
            ListTile(title: Text("我是关注")),
            ListTile(title: Text("我是关注")),
            ListTile(title: Text("我是关1注")),
            ListTile(title: Text("我是关2注")),
            ListTile(title: Text("我是关注")),
            ListTile(title: Text("我是关23注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关注")),
            ListTile(title: Text("我是关124注")),
            ListTile(title: Text("我是关14注")),
            ListTile(title: Text("我是关412注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关4注")),
            ListTile(title: Text("我是关422注")),
          ],
        ),),
        
        const Text("我是热门"),
        const Text("视频"),
        const Text("娱乐"),
        const Text("篮球"),
        const Text("深圳"),
        const Text("疫情"),
        const Text("其他"),
      ]),
    );
  }
}
